<?php

namespace app\api\model;

use think\Model;

class UserInfo extends Model
{

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 他的主页
     * @param int $user_id 用户ID
     * @param int $home_id 他的用户ID
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function homePage($user_id, $home_id)
    {
        if (empty($home_id) || !$home_id) {
            $home_id = $user_id;
        }
        $data = $this->where('user_id', $home_id)->find();
        if (empty($data)) return '';
        $date = date('Y', time());
        $data['age'] = (int)$date - (int)substr($data['birthday'], 0, 4);//年龄
        $data['attention'] = Attention::get(['user_id' => $user_id, 'attention_id' => $home_id]) ? 1 : 0;
        $data['friends'] = Friends::get(['user_id' => $user_id, 'friends_id' => $home_id]) ? 1 : 0;
        $data['photo'] = Photo::all(['user_id' => $home_id]);
        $user = model('user')->where('id', $home_id)->find();
        $data['user_invitation_code'] = $user['user_invitation_code'];
        $data['ry_token'] = $user['ry_token'];
        $data['members'] = Members::get(['user_id' => $home_id, 'status' => 1]) ? 1 : 0;
        $data['address'] = $data['province'] . $data['city'] . $data['area'];
        return $data;
    }

    /**
     * 我的信息
     * @param $user_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myInfo($user_id)
    {
        $data = $this->where('user_id', $user_id)
            ->field('id,username,avatar,gender,birthday,signature,status,real_name,card_number')->find();
        $data['friends_ver'] = model('user')->where('id', $user_id)->value('friends_ver');
        $data['members'] = model('members')->where('user_id', $user_id)->value('status') ?? 0;
        $data['age'] = user_age($data['birthday']);//年龄
        $data['friends'] = model('friends')->where('user_id', $user_id)->count() ?? 0;//朋友数量
        $data['group'] = model('group_user')->where('user_id', $user_id)->count() ?? 0;//群组数量
        $data['attention'] = model('attention')->where('user_id', $user_id)->count() ?? 0;//关注数量
        $data['fans'] = model('attention')->where('attention_id', $user_id)->count() ?? 0;//粉丝数量
        $data['see'] = model('see_me')->where('see_id', $user_id)->count();//查看我数量
        return $data;
    }

    /**
     * 我的好友（关注，粉丝，群组）
     * @param $user_id
     * @param $type  int 1:好友  2：关注  3：粉丝  4：群组
     * @param $search string 搜索内容
     * @return \think\Paginator
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function myCommunication($user_id, $type, $search)
    {
        if ($type == 1) {
            $model = 'friends';//查找所属model
            $opp_id = 'a.friends_id';//对象ID
            $where['a.user_id'] = ['eq',$user_id];//判断条件
        } else if ($type == 2) {
            $model = 'attention';//查找所属model
            $opp_id = 'a.attention_id';//对象ID
            $where['a.user_id'] = ['eq',$user_id];//判断条件
        } else if ($type == 3) {
            $model = 'attention';//查找所属model
            $opp_id = 'a.user_id';//对象ID
            $where['a.attention_id'] = ['eq',$user_id];//判断条件
        }else{
            //查找群组
            $where = [];
            if($search){
                $where['b.group_name'] = ['like', '%'.$search.'%'];
            }
            $data = model('group_user')
                ->alias('a')
                ->join('group b', 'a.group_id = b.group_id')
                ->where('a.user_id', $user_id)
                ->where($where)
                ->field('a.group_id,auth,group_name,introduction,avatar')
                ->paginate(10);
            foreach ($data as $k=>$v){
                $data[$k]['num'] = model('group_user')->where('group_id', $v['group_id'])->count() ?? 0;
            }
            return $data;
        }
        if($search){
            $where['b.username'] = ['like', '%'.$search.'%'];
        }
        $data = model($model)
            ->alias('a')
            ->join('user_info b', $opp_id.'=b.user_id')
            ->where($where)
            ->field('b.user_id,b.avatar,b.gender,b.username,b.status as auth_status,b.birthday,b.signature')
            ->paginate(10);
        foreach ($data as $k => $v) {
            $data[$k]['user_age'] = user_age($v['birthday']);
            $data[$k]['distance'] = '12km';
            $data[$k]['members'] = model('members')->where('user_id', $v['user_id'])->value('status') ?? 0;
        }
        return $data;
    }
}